<template>
	<view class="content">
		<open-data class="logo" type="userAvatarUrl"></open-data>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<button class="btn" @tap="login">授权登入</button>
	</view>
</template>

<script>
	const {appLogin,httpGet,httpPost,msg,ZC} = require("@/utils/common.js")
	const app = getApp();
	export default {
		data() {
			return {
				title: '欢迎使用师生管家',
				code:'',
			}
		},
		onLoad() {
		},
		methods: {
			getCode(){
				return new Promise((resolve, reject)=>{
					wx.login({
						success:(res)=>{
							resolve(res.code)
						}
					})
				}) 
			},
			login(e){
				wx.getUserProfile({
					desc:"微信登入注册",
					success:(res)=>{
						let iv = res.iv
						let encryptedData = res.encryptedData
						res.userInfo = JSON.stringify(res.userInfo)
						this.getCode().then((code)=>{
							res.code = code
							httpPost({
								url:'users/routineLogin',
								data:res
							}).then((res)=>{
								console.log(res)
								app.globalData.user_info = res.result
								app.globalData.user_token = res.result.token
								ZC('user_info',app.globalData.user_info,-1)
								ZC('user_token',app.globalData.user_token,-1)
								
								var url = uni.getStorageSync('login_back_url', url);
								uni.removeStorage({key:'login_back_url'});
								// uni.navigateBack()
								uni.redirectTo({
									url:url||'../index/index',
									fail(res) {
										console.log(res)
									}
								})
							})
						})
					},
					fail:(err)=>{
						
							console.log(err)
					}
				})
				
				// wx.getUserInfo({
				// 	success: function(res) {
				// 	    console.log('user_info',res);
				// 	}
				// })
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		text-align: center;
		margin-top:50rpx;
		
		font-size: 40rpx;
		letter-spacing: 10rpx;
		
	}
	.btn{
		margin-top: 50rpx;
		width: 600rpx;
		height: 115rpx;
		border-radius: 65rpx;
		line-height: 115rpx;
		background-color: #3085ff;
	}
</style>
